#!/bin/bash
# usage: testpythonio filename timeout judgetype
# should be run from the directory private/test_to_execute/<unique random hash>
#
# etst configuration variables:
#
OUTPUT_FILE=test.out
PYTHON_ARGS=""
PROGRAM_OUTPUT_FILE=program.out
#
#
mkdir out 2> /dev/null
chgrp testovac out
chmod g+rwx out
chgrp testovac .
chmod g+r .
#
echo "<pre>" > $OUTPUT_FILE
#
echo "<br />Running:<br />" >> $OUTPUT_FILE
cp -r * out 2> /dev/null
rm -f out/$OUTPUT_FILE 2> /dev/null
rm -rf out/test_data 2> /dev/null
chgrp testovac out/*
cd out
echo "------------------------Testing code $1.py...<br />" >> ../$OUTPUT_FILE
sudo -u testovac /opt/lampp/htdocs/list-svn/dev/test_scripts/testpythonio2 $1 > ../$PROGRAM_OUTPUT_FILE 2>> ../$OUTPUT_FILE
STATUS=$?
cd .. ;
#
rm -rf out 2> /dev/null
if [[ $STATUS == 0 ]] ; then
	if [[ "$3" == "judge-type-diff" ]] ; then
		diff test_data/test_target.txt program.out > /dev/null 2> /dev/null
		JUDGE_STATUS=$?
		if [[ $JUDGE_STATUS == 0 ]] ; then
			echo "------------------------Result matches expected target!<br />" >> $OUTPUT_FILE
			echo "</pre>" >> $OUTPUT_FILE
			exit 0
		else
			echo "------------------------Result is else that expected target!<br />" >> $OUTPUT_FILE
			echo "</pre>" >> $OUTPUT_FILE
			exit 1
		fi
	fi
	if [[ "$3" == "judge-type-script" ]] ; then
		cd test_data
		cat ../program.out | nice /opt/lampp/htdocs/list-svn/dev/test_scripts/run_with_timeout 300000 /usr/bin/python test_judge.py test_input.txt >> $OUTPUT_FILE 2>> $OUTPUT_FILE
		JUDGE_STATUS=$?
		cd .. ;
		if [[ $JUDGE_STATUS == 0 ]] ; then
			echo "------------------------Result pass judge script successfully.<br />" >> $OUTPUT_FILE
			echo "</pre>" >> $OUTPUT_FILE
			exit 0
		else
			echo "------------------------Result failed to pass judge script.<br />" >> $OUTPUT_FILE
			echo "</pre>" >> $OUTPUT_FILE
			exit 1
		fi
	fi
	echo "------------------------NO JUDGER SPECIFIED!<br />" >> $OUTPUT_FILE
	echo "</pre>" >> $OUTPUT_FILE
	exit 10
else
	echo "------------------------RUN TIME ERROR!<br />" >> $OUTPUT_FILE
	echo "</pre>" >> $OUTPUT_FILE
	exit 100
fi
